$KCODE = 'u'
require 'iconv'

class Category
  attr_accessor :name,:sub_categories

  def initialize(name,sub_categories)
    @name = name
    @sub_categories = sub_categories
  end

  def get(sub_category_name)
    @sub_categories.each do |sub_category|
      if sub_category.name == sub_category_name
        return sub_category
      end
    end
    sub_category = SubCategory.new(sub_category_name,[])
    @sub_categories << sub_category
    sub_category
  end
end

class SubCategory
  attr_accessor :name,:products

  def initialize(name,products)
    @name = name
    @products = products
  end

  def add(product)
    products << product
  end
end

class Product
  attr_accessor :quantity,:no,:price
  attr_reader :name,:category,:sub_category,:content,:pic,:color

  def initialize
    @quantity = "999"
  end

  def name=(name)
    @name = convert(name)
  end

  def category=(category)
    @category=convert(category)
  end

  def sub_category=(sub_category)
    @sub_category=sub_category
    @sub_category = "no sub category" unless @sub_category
  end

  def content=(content)
    @content=content
  end

  def pic=(pic)
    if pic
      ss = pic.split('/')
      if(ss.length == 2)
        @pic = ss[1]
      end
    end
  end

  def color=(color)
    @color = convert(color)
  end



  def convert(s)
    s
  end
end

class ProductContainer
  attr_reader :categories
  
  def initialize
    @categories = []
  end

  def get(category_name)
     @categories.each do |category|
      if category.name == category_name
        return category
      end
    end
    category = Category.new(category_name,[])
    @categories << category
    category
  end

  def add_product(product)
    category = get(product.category)
    sub_category = category.get(product.sub_category)
    sub_category.add product
  end
end